Using atomic sets of memory locations

ABSTRACT

A system and method for ensuring consistency of data and preventing data races, including steps of: receiving and examining a computer program written in an object-oriented language; receiving sequences of accesses that form logical operations on a set of memory locations used by the program; receiving definitions of atomic sets of data from the memory locations, wherein said atomic sets are sets of data that indicate an existence of a consistency property without requiring the consistency property itself; inferring which code blocks of the computer program must be synchronized in order to prevent one or more data races in the computer program, wherein synchronization is inferred by determining by analysis for each unit of work, what atomic sets are read and written by the unit of work; and providing a message indicating where synchronization is required.

CROSS-REFERENCE TO RELATED APPLICATIONS

This application is a continuation of, and claims priority from,commonly-owned, co-pending U.S. patent application Ser. No. 11/150,611,filed on Jun. 10, 2006, which is incorporated by reference as if fullyset forth herein.

STATEMENT REGARDING FEDERALLY SPONSORED-RESEARCH OR DEVELOPMENT

Patent application Ser. No. 11/150,611, from which this applicationclaims priority, was made with Government support under contract No.NBCH3039004 awarded by DARPA (PERCS Phase II). The United StatesGovernment has certain rights under the invention.

INCORPORATION BY REFERENCE OF MATERIAL SUBMITTED ON A COMPACT DISC

None.

FIELD OF THE INVENTION

The invention disclosed broadly relates to the field of informationprocessing, and more particularly relates to the field of softwaredevelopment and debugging.

BACKGROUND OF THE INVENTION

Data races are intermittent bugs that arise in concurrent programs. Theyresult in incorrect behavior under certain interleavings of the code,and are hard to find and debug. The most common definition of a datarace is two concurrent accesses to a memory location with nosynchronization between them where at least one of which is a write.This definition has two shortcomings. First, there is a circularity inthat synchronization is what we use to prevent data races, so definingraces in terms of synchronization is unsatisfying. Second, a program inwhich every access is protected can nevertheless display problematicinterleavings. This is the case when the granularity of locking is notadequate. An example of these “high-level” data races is stale-valueerrors, local copies of shared variables that are used beyond anappropriate scope. See M. Burrows and K. R. M. Leino. “Findingstale-value errors in concurrent programs”. SRC Technical Note 2002-004,May 2002. Therefore there is a need for a system and method of ensuringconsistency of data and preventing data races that avoids theshortcomings of prior approaches.

SUMMARY OF THE INVENTION

Briefly, according to an embodiment of the invention a system and methodincludes steps or acts of receiving and examining a computer programwritten in an object-oriented language; receiving sequences of accessesthat form logical operations on a set of memory locations used by theprogram; receiving definitions of atomic sets of memory locations, eachconsisting of pieces of data; providing a message indicating where thesynchronization is required.

BRIEF DESCRIPTION OF THE DRAWINGS

To describe the foregoing and other exemplary purposes, aspects, andadvantages, we use the following detailed description of an exemplaryembodiment of the invention with reference to the drawings, in which:

FIG. 1 shows a class shape.

FIG. 2 shows the result of a synchronization inference, which added twolocks, loc_lock and dim_lock for each of the atomic sets.

FIG. 3 shows a complete set of interleaving scenarios that areproblematic.

FIG. 4 illustrates some of the scenarios where memory is updatedinconsistently.

FIG. 5 gives a sample execution and its timestamps.

FIG. 6 provides the syntax of atomic set constructs.

FIG. 7 shows pseudocode for the first step in a synchronizationinference.

FIG. 8 is a flowchart of a method according to an embodiment of theinvention.

While the invention as claimed can be modified into alternative forms,specific embodiments thereof are shown by way of example in the drawingsand will herein be described in detail. It should be understood,however, that the drawings and detailed description thereto are notintended to limit the invention to the particular form disclosed, but onthe contrary, the intention is to cover all modifications, equivalentsand alternatives falling within the scope of the present invention.

DETAILED DESCRIPTION

According to an embodiment of the invention we use a program written inan object-oriented language such as Java. A new language constructallows the programmer to express what fields form atomic sets, meaningthat there exists a consistency property among these memory locations,and that they must be accessed and updated atomically (i.e., withoutinterruption). Based on this information, an inference engine analyzesaccesses to these atomic sets and infers appropriate synchronizationblocks in the code automatically.

The inference engine assumes the existence of units of work, code blocksthat mark transactional boundaries. In Java, the methods of a classnaturally form units of work for all atomic sets declared in that class.The analysis builds call graphs rooted at each of these, and propagatesaccesses to an atomic set in a bottom-up fashion. Synchronization isadded to the code to ensure that in any execution, units of work areserializable for each atomic set, i.e. for each the execution isequivalent in behavior to one where all units of work happened in someserial order. To this end, we have developed a new definition forhigh-level data races, consisting of a complete set of non-serializableaccess patterns, and synchronization is inferred in such a way so as toprevent these interleaving scenarios. A proof establishes that anexecution that does not display any of these patterns has the propertythat for each atomic set, there exists a serial order of units of work,as required.

According to this embodiment of the invention, a new definition forhigh-level data races is presented that is based on an adaptation ofanomalies used to characterize different isolation levels in databases.See ANSI X3.135—1992, American National Standard for InformationSystems—Database Language—SQL, November 1992. Commercial databases allowprogrammers to trade off consistency for performance by offeringdifferent levels of isolation. Each level is characterized by the set ofanomalies it does not allow. The highest level of isolation isserializability, where an execution is equivalent in behavior to onewhere all transactions have been executed in some serial order. We adaptthe anomalies to the context of concurrent programming. Our definitionof data races is based on possible non-serializability.

This definition covers both high-level and low-level data races, andgeneralizes the common definition in several respects. First, for asingle memory location it identifies interleaving scenarios ofconcurrent accesses that are problematic. Database anomalies are alsogiven as schedules, and this helps to give a more precise account ofwhat constitutes a problematic interleaving, while avoiding the use ofimplementation constructs such as locks or synchronization. Second, ittakes into account accesses to a set of memory locations that need to beupdated atomically due to a consistency property, i.e. an atomic set.Our definition assumes the existence of units of work on atomic sets,which are a sequence of accesses forming a logical operation on thewhole set. Units of work mark transactional boundaries, and whenexecuted sequentially, restore the atomic set to a consistent state. Ourdefinition overcomes the shortcomings of the common one in that itcovers a larger class of problematic interleavings, it leaves out somebenign cases that are considered as races by the low-level definition,and it is not based on an implementation construct. Both stale-valueerrors, and view consistency violations, where multiple reads in anoperation observe an inconsistent state, are covered. See M. Burrows andK. R. M. Leino. “Finding stale-value errors in concurrent programs”. SRCTechnical Note 2002-004, May 2002 and C. Artho, K. Havelund, and A.Biere. “High-Level Data Races”, In Workshop on Verification andValidation of Enterprise Information Systems (VVEIS), April 2003.

Our definition is given in terms of problematic interleaving scenariosfor accesses to a single or a pair of memory locations. They can beapplied pair-wise to atomic sets having more than two locations. We showthat these scenarios are complete, in the sense that, if an executiondoes not display them then it satisfies a desirable property: itsprojection on each atomic set is serializable, meaning that if we onlyconsider accesses to that atomic set; the execution is equivalent inbehavior to one where units of work occur in a serial order. Thisproperty does not imply that the execution is serializable across allatomic sets, but that for each set, there exists a serial order of unitsof work. The intuition behind this correctness criterion is that we wanteach set to remain consistent, without reducing the amount ofconcurrency unnecessarily.

The new definition has several applications: (i) detecting data races,including high-level data races; (ii) uncovering unnecessarysynchronization; and (iii) inferring which code blocks must besynchronized to prevent data races. In the embodiment discussed, wefocus on the third application in the context of Java, presenting asystem that infers synchronization for an API (application programinterface). We now discuss a new language construct that allows theprogrammer to mark sets of fields of a class as forming atomic sets, andassume that the public and protected methods of that class form units ofwork for those sets. Our system uses this information to automaticallyadd appropriate synchronization to that class. Note that if a method isa unit of work, that does not necessarily mean that its entire bodyneeds synchronization. The units of work only indicate the boundaries oflogical operations, and our system aims at finding the smallest codeblocks within them that need synchronization. This application is usefulwhen there are multiple independent sets of fields. The traditionalapproach is to have various locks corresponding to each set to allowmore concurrency. This requires the programmer to keep track of all thelocks, which can be error-prone, especially in the presence ofsubclassing. Atomic sets of fields are a high-level construct thatremove some burden from the programmer, and help in producing correctsynchronization.

The embodiment discussed herein (for purposes of illustration) works bybuilding a call graph for an API, taking all public and protectedmethods as entry points. We do not rely on any particular call graphconstruction algorithm. The atomic sets accessed at each node arepropagated from callees to callers, and the code blocks that needsynchronization are detected based on the scenarios of problematicaccesses, identified in our definition for high-level data races. As anapproximation for this embodiment, we add synchronization to thesmallest code blocks that surround all reads or writes to an atomic setin a unit of work, ignoring the intricacies of the scenarios.

FIG. 1 shows a class called Shape, which has two fields to indicatelocation, x and y, and two for dimension, width, and height. Thesefields form independent sets, and are updated separately with methodsadjustLocation and adjustDimension. To make this class thread-safe, wecould make each method synchronized, but that would decrease the amountof concurrency unnecessarily, because one could no longer update thelocation and dimensions—two orthogonal aspects—of Shape concurrently.While we are updating x and y, we do not want intermediate states to bevisible. The atomic set construct allows us to express these propertiesdirectly as shown in FIG. 1. The modifier atomic(loc) on a fielddeclaration indicates that the field belongs to atomic set loc, whereloc is a label. Labels must be globally unique. Thus this class declarestwo sets loc and dim.

Atomic sets alone are not enough to determine what code fragment needssynchronization. They indicate the existence of a consistency propertywithout requiring the property itself. We must therefore know whatsequences of accesses form logical operations, or units of work, on thewhole set, restoring it to a consistent state when executedsequentially. The goal of synchronization is to then make sure that foreach atomic set, units of work are serializable, meaning that theireffect on an atomic set is as if they were executed in some serialorder. To simplify the job of the programmer, we make the assumptionthat the public and protected methods of an API are units of work foreach of the atomic sets declared in that class, and do not require thisinformation to be supplied directly.

FIG. 2 shows the result of a synchronization inference, which adds twolocks, loc_lock and dim_lock for each of the atomic sets. The printmethod has an update access to atomic set loc (the call toadjustLocation), and two reads, and it includes all these accesses inthe same synchronized block (similarly for the dim set). This preventsvarious high level data races. For example, if the reads of x and y arenot in the same synchronized block, then they can observe a state of thetwo locations that never existed in memory: a value of x from oneupdate, and one for y from another one. Also if adjustLocation is placedoutside the synchronized block, then the reads can see a statecorresponding to a different update, and that would result in a possibleexecution that is not serializable.

We now discuss a definition of high-level data races according to anembodiment of the invention. Consider an abstract model with a set ofmemory locations, where each location can be read and updateduninterruptedly by a set of threads. A thread is a sequence of reads andwrites to individual memory locations, where each access belongs to someunit of work. Locations l1 and l2 are two locations forming an atomicset. We want to know under what circumstances l1 and l2 are read andupdated inconsistently, i.e. what scenarios make it possible forintermediate states of updates to be observable, and result in nonserializable executions. Some of the scenarios we present bearresemblance to database anomalies that characterize different levels ofisolation in the ANSI SQL standard. In the following section, we showthat these scenarios are complete, provided that each unit of work thatdoes any write to an atomic set, writes all the locations in that set.

Let LS be the set of all memory locations. A subset L⊂

may be designated as atomic. An event is an access to a memory locationlε

. Accesses can be a read R(l) or a write W(l). We assume that accessesto a single memory location are uninterrupted. The notation R(l)indicates a read of one of the memory locations in L, and similarly forW(l). If l denotes locations l1 or l2, we use the notation L−1 to denotethe other location.

A thread is a sequence of events. Subsequences of a thread form units ofwork. These are non-overlapping, and every event belongs to some unit ofwork. We write Ru(l) for a read performed by unit of work u of somethread t, and similarly for writes. The notation thread(u) denotes thethread corresponding to u. Note that a unit of work can represent alogical operation for more than one atomic set. An execution is asequence of events from one or more threads. An interleaving scenario isalso a sequence of events. For example, Ru(l) Wu′(l) Wu(l) is aninterleaving scenario where unit of work u first reads l, then anotherunit of work u′, from another thread, performs a write, followed by awrite by u. An execution is in accordance with an interleaving scenarioif it contains the events in the interleaving scenario, and these appearin the same order.

Referring to FIG. 3, there are shown a number of interleaving scenariosthat are considered problematic. To define High-Level Data Races, we letL be an atomic set of locations, l1, l2εL, l one of l1 or l2, and u andu′ two units of work for L, such that thread(u)≠thread(u′). An executionhas a data race if it is in accordance with one of the interleavingscenarios of FIG. 3.

We now consider the problematic interleaving scenarios and describeinformally why these scenarios are problematic. These scenarios arecomplete provided that each unit of work that writes to an atomic set,writes all locations in that set. In the first scenario, unit of work ureads one location l, followed by an update to l. If another update to lis interleaved between the two, then the read operation yields a stalevalue and the subsequent update may be inconsistent. This scenariocaptures common “low-level” data races, such as two threads doing x++.Scenario 1 corresponds roughly to the “lost update” ANSI.X3.135 anomalyin databases: a transaction T1 reads a data item, then anothertransaction T2 updates the item, then T1 updates the item based on thevalue read and commits. The update of T2 is then lost.

The second scenario shows two consecutive reads of location l in a unitof work that do not yield the same value. It roughly corresponds to the“fuzzy read” anomaly in databases, where a transaction T1 reads a dataitem, then a second transaction T2 modifies that item and commits. If T1attempts to re-read the same item, it receives a different value.

In a third scenario 3, an intermediate value of 1 is read, when a unitof work writes it multiple times. In scenario 4, the value read for 1 isnot the same as the one last written in the same unit of work. Inscenario 5, a write to l is lost, or hidden by the writes from some unitof work.

Referring now to FIG. 4, we illustrate scenarios 6 and 9 of FIG. 3.Scenarios 6 and 7 illustrate cases where memory is updatedinconsistently. Recall that l denotes one of l1 or l2, and that L−1denotes the other. In scenario 6, a unit of work updates some locationin the set, followed by an update to another location. Thus the wholeset updated in multiple steps. If a write to the set is interleavedbetween the two, then memory is left in an inconsistent state sinceindividual locations have values from different operations. A reader maythen observe what appears to be intermediate states of various updates.Scenario 7 is similar.

Scenarios 8 through 11 (FIG. 4) illustrate cases where memory is readinconsistently, even if it may never have been written incorrectly. Inscenario 9, one unit of work reads l1 followed by reading l2. Thus onethread is observing the state of multiple parts in the atomic set. If anupdate to the whole set is interleaved, then the values observed belongto different operations. The rest of the scenarios are problematic for asimilar reason. These scenarios are similar to the “read skew” databaseanomaly.

All scenarios that only manipulate one memory location are marked ashaving a data race by the common definition. However, there are threescenarios missing Ru(l) Ru′(l) Ru(l), Wu(l) Ru′(l) Ru(l), and Ru(l)Ru′(l) Wu(l). None of these are problematic, but the common definitionmarks the last two as having a race. Our definition avoids these benigncases. An example of the third scenario is a thread performing x++ andanother printing the value of x, where the write of x is atomic. This isnon-deterministic but serializable, so there is no data race.

Not all of the database anomalies are applicable in this context. Someare concerned with an erroneous behavior when a transaction aborts androlls back: e.g. “dirty read” and “dirty write”. Others refer to readinga set of memory locations that satisfy a search condition: “phantomread.” Finally the “write skew” anomaly is covered by several of ourscenarios.

We now show that the interleaving scenarios are complete, meaning thatif an execution does not display them, then its projection on eachatomic set is serializable, concepts that we define precisely below. Tothis end, we introduce a formal model of timestamps. Units of work canbe totally ordered by the occurrence of their first write events in anexecution. We associate a unique timestamp with each unit of work,respecting this order. A write event gets the timestamp of the unit ofwork to which it belongs. A read event gets the timestamp of the mostrecent write to the memory location it is reading. If a memory locationgets written more than once by a unit of work, we mark the location astemporary, until the last write is completed. We use timestamps andtemporary locations to capture consistency: if two reads get differenttimestamps, they are observing an inconsistent state. Likewise,observing a location marked as temporary by another unit of work isundesirable. We make the following assumption in our determination ofcompleteness:

We assume that each execution is such that every unit of work thatwrites some location in an atomic set, writes every location in thatatomic set. This assumption is not restrictive because we can always add“dummy writes” to any unit of work that does not satisfy it. Since thisframework is oblivious to actual values written, the values written bythese dummy writes do not matter. Furthermore, these dummy writes areneeded only for analysis, and would be added only to the programrepresentation being analyzed.

For an execution that is not in accordance with the interleavingscenarios of Definition 1, we show that the timestamp of writes to agiven memory location are monotonically increasing (Lemma 1), and thatno unit of work observes an inconsistent state (Lemma 2). These twoproperties suffice to show that the execution is serializable whenprojected on each atomic set (Theorem 1), using the SerializabilityTheorem from database theory.

Indices refer to a total order of events in an execution that is clearfrom context. A total order of events is natural for a sequentiallyconsistent architecture. However, events happen in some total order evenon weaker memory models, so our conceptual model is still applicable.

The function event(i) gives the event at index i in the execution. If uis a unit of work, then firstWrite(u) is the index of the first writeevent of u.

We assume that timestamps are drawn from the natural numbers, and thatthe indices in an execution start at 1. We use ts(u) to denote thetimestamp of a unit of work that performs writes. We allocate timestampsto units of work in such a way that:

ts(u)<ts(u′)

firstWrite(u)<firstWrite(u′).

So a unit of work u, whose first write happens before the first write ofanother unit of work u′ in an execution, gets a lower timestamp. Given atotal order of timestamps thus allocated, let prev(t) be the timestampimmediately preceding t in this order (prev(t)<t).

We associate a timestamp, ts(i), with an event at index i in theexecution. Write events get the timestamp of the unit of work to whichthey belong, and read events get the timestamp of the most recent writeto the memory location read. ts(i) is computed as follows:

${{ts}(i)} = {\begin{matrix}{{ts}(u)} & {{{if}\mspace{14mu} {{event}(i)}} = {W_{u}(1)}} \\{{ts}(j)} & {{{if}\mspace{14mu} {{event}(i)}} = {{{{R_{u}(1)}\bigwedge j} < {i\bigwedge{{event}(j)}}} = {W_{\alpha^{\prime}}(1)}}} \\\; & {{{\bigwedge{\alpha \; {k \cdot j}}} < k < {i\; \xi \mspace{14mu} {{event}(k)}}} = {W_{\alpha} \cdot (1)}} \\0 & {{{if}\mspace{14mu} {{event}(i)}} = {{{{{R_{u}(1)}\bigwedge\alpha}\; {j \cdot j}} < {i\; \xi \mspace{14mu} {{event}(j)}}} = {W_{\alpha^{\prime}}(1)}}}\end{matrix}}$

So far ts(u) is only defined for units of work u that perform writes.For a unit of work that consists entirely of read events, letts(u)=ts(i) for some i such that event(i)=Ru(l).

The predicate temp(u,i,l) is true if location l is temporary for unit ofwork u at index i, meaning that there will be another write to l in ubeyond index i. It is false for unit of work u at index i if irepresents the index of the last write to l in u. For reads, we take thevalue of temp(u,i,l) to be the value temp(u,j,l), where j is the indexof the last write by u to l. Initially, temp(u,0,l)=false, for all u andl. It is computed as follows:

${{temp}\left( {u,i,1} \right)} = {\begin{matrix}{true} & {{{if}\mspace{14mu} {{event}(i)}} = {{{W_{\alpha}(1)}\bigwedge{\exists{j > {i\; \xi \mspace{14mu} {{event}(j)}}}}} = {W_{\alpha}(1)}}} \\{false} & {{{if}\mspace{14mu} {{event}(i)}} = {{{{{W_{\alpha}(1)}\bigwedge\alpha}\; j} > {i\; \xi \mspace{14mu} {{event}(j)}}} = {W_{\alpha}(1)}}} \\{{temp}\left( {u,i,1,1} \right)} & {otherwise}\end{matrix}}$

FIG. 5 shows a sample execution and its timestamps. In this example,there are two units of work u1 and u2 in different threads, and twolocations l1 and l2. We have ts(u1)=1 and ts(u2)=2. At index 2, unit ofwork u2 reads an intermediate value of location l1. This is captured bytemp(u1,2,l1) being true.

The following lemma (Lemma 1) states that the timestamps of write eventson the same memory location l are monotonically increasing: In anexecution that is not in accordance with the interleaving scenarios ofDefinition 1, if i and j are such that i<j, event(i)=Wu(l) andevent(j)=Wu′(l) for some l, and u u′, then ts(i)<ts(j).

A second lemma (Lemma 2) states that the state observed in a unit ofwork is consistent, by giving three properties of read events in anexecution that is not in accordance with any of the scenarios inDefinition 1. First, no temporary value is ever read. Second, reads in aunit of work that also writes the same atomic set do not get stalevalues. Third, two reads in a unit of work that does not perform anywrites to same atomic set get consistent values.

Thus, according to Lemma 2, in an execution that is not in accordancewith any of the interleaving scenarios of Definition 1:

(1) No event from one unit of work reads a memory location marked astemporary by another unit of work:

∀u,i,lξevent(i)=R _(u)(l)

αu′≠uξtemp(u′,i,l).

(2) A read in a unit of work that also contains a write to the sameatomic set does not get a stale value, i.e., it gets the timestampcorresponding to the unit of work or the previous one:

∀iξ(event(i)=R _(u)(l)

∃jξevent(j)=W _(u)(l))

ts(i)ε{ts(u),prev(ts(u))}.

(3) Reads in a unit of work u that does not contain writes, get the sametimestamp.

∀ijξ(event(i)=R _(u)(l)

event(j)=R _(u)(l′)

αkξevent(k)=W _(u)(l))

ts(i)=ts(j)=ts(u).

Finally, we determine that for an execution that is not in accordancewith any of the interleaving scenarios of Definition 1, its projectionon each atomic set is serializable, which we define precisely below. Wecan think of a unit of work as being a single threaded transaction thatalways commits, and this allows us to use concepts from serializabilitytheory. See: P. Bernstein, V. Hadzilacos, N. Goodman. “ConcurrencyControl and Recovery in Database Systems”, Addison-Wesley, 1987. Givenan execution E and an atomic set L, the projection of E on L is anexecution that has every event on L in E in the same order. Givenindices i and i′ such that i<i′, the pair (event(i), event(i′)) is aconflicting pair of events, if they are on the same memory location andone of them is a write. We say that two executions are equivalent ifthey consist of the same units of work and the same events, and have thesame pairs of conflicting events. An execution is serial if for everytwo units of work u and u′ that appear in it, either all events in uhappen before all events in u′, or vice versa. We say that an executionis serializable if it is equivalent to an execution that is serial.

The conflict graph of an execution is a directed graph, with nodesconsisting of units of work. There is an edge between units of work uand u′, if u and u′ have events e and e′, respectively, such that (e,e′)is a conflicting pair. The Serializability Theorem states that anexecution is serializable, if and only if its conflict graph is acyclic.P. Bernstein, V. Hadzilacos, N. Goodman. “Concurrency Control andRecovery in Database Systems”, Addison-Wesley, 1987. We use this fact toprove Theorem 1 (serializability): For an execution that is not inaccordance with any of the interleaving scenarios of Definition 1, itsprojection on each atomic set is serializable.

We now discuss how the abstract model of the previous discussion can beapplied to an actual programming language such as Java. We introduce aprogramming construct for marking fields of an API as forming an atomicset, and assume that its public and protected methods are units of workfor those sets, excluding any constructors. The syntax of atomic setconstructs is given in FIG. 6. The modifier atomic(label) indicates thatthe field being declared belongs to atomic set named label. Labels areglobally unique identifiers. Fields can belong to at most one set.Objects in such fields must not be used as monitors, meaning that thereare no wait or notify performed on them. Atomic sets must consistentirely of instance fields or entirely of static ones.

The atomic set construct guarantees to the programmer that nointermediate state of an atomic set in a unit of work is observed byothers. It is therefore a statement about the visibility of intermediatestates. Referring again to FIG. 1, marking x and y as being part ofatomic set loc ensures that when adjustLocation is executed, theintermediate state (x, y)=(1.0, 0.0) cannot be observed by other publicmethods. On the other hand, the four fields belong to two differentatomic sets, so intermediate states of print involving a consistentstate of (x, y) and a consistent state of (width, height) areobservable. Consider two print methods executing concurrently, startingfrom a state where all fields are 0.0. It is possible for the first oneto print “x=1.0 y=1.0 width=2.0 height=2.0”, and for the second one toobserve the state: (x, y, width, height)=(1.0, 1.0, 0.0, 0.0) and print“x=2.0 y=2.0 width=1.0 height=1.0”. This happens if the first printmethod executes everything before the call to adjustDimension, and thenthe second one executes entirely, followed by the rest of the first one.However, if we declare all four fields to be part of an atomic set, thenthis is no longer possible. The state: (x, y, width, height)=(1.0, 1.0,0.0, 0.0) then becomes an intermediate state of that atomic set in unitof work print and must not be observed by other units of work. Thus bygrouping more fields in an atomic set, we reduce the number ofintermediate states observable, as well as the amount of concurrency.

Fields should comprise an atomic set when they have some commonconsistency property, i.e. must be updated atomically. The construct isespecially useful when there are multiple independent fields in an API.The traditional approach is to declare a lock for each. The programmermust keep track of all the locks, and add synchronization to the codeappropriately, which can be error-prone. The atomic set construct helpsto reduce the burden on the programmer by inferring synchronizationautomatically. Moreover, it allows for better extensibility, by makingit easier for the programmer to change the level of concurrency. In thetraditional approach, all the synchronized blocks need to be found andchanged manually. In our approach, however, only a few declarations needto be changed—including or removing fields from atomic sets—in order tomodify the amount of concurrency.

It is possible for the programmer to make mistakes in declaring atomicsets, which may result in allowing more concurrency, and violations ofcorrectness specifications. However, we believe that it is easier tomark fields as atomic, than to add synchronization manually. This isbecause a lot of the reasoning that goes into allocating locks todifferent subsets of the fields of a class is the same as what is neededfor figuring out which fields must be in an atomic set. Our constructallows the programmer to express this directly, and rely on our analysisengine to find all the places where synchronization is needed.

We now discuss a synchronization inference engine according to theinvention. In order to use this tool, the programmer needs to be awareof some assumptions. First, it is intended to add proper synchronizationto an API which owns its representation. This is the case for any,well-designed API, and we found it to hold for all examples considered.In particular, fields included in atomic sets must be internal to theAPI, meaning that they are owned by it and do not escape. This isbecause we use synthetic locks for each atomic set, and do not holdlocks directly on the fields. So if the object referenced by a field isshared with another entity that holds a lock on it, then our syntheticlocks may not provide the right synchronization. Inner classes thatrequire synchronization on fields declared in the containing class mustalso be internal. Second, we require a complete API, in the sense thatthe types it references must be available to the analysis.

Synchronization inference works in two steps. First, an analysisdetermines for each unit of work, what atomic sets it reads and writes.Second, this information is used to determine the smallest code blockthat needs synchronization within each unit of work. For thisapplication, we ignore the intricacies of the problematic interleavingscenarios, and add synchronization to code blocks surrounding all readsand writes to an atomic set in a unit of work. This is a veryconservative approximation but turned out to be sufficient for ourexamples. For a different application, such as high-level data racedetection, the intricacies of the interleaving scenarios would need tobe taken into account.

FIG. 7 shows the pseudocode for the first step in synchronizationinference. SCC denotes a strongly connected component. The first step isto compute, for each unit of work and each atomic set, the fields of thegiven set that the given unit of work reads and writes. This can be doneas a single bottom-up pass over the call graph of each unit of work,assigning to each node in the graph two sets of fields—one for reads andone for writes—comprised of those fields accessed in that node and thoseaccessed by its callees. This is shown in FIG. 7; the local analysis ofa method (LocalReads(S) and LocalWrites(S)) simply traverses the bodyrecording explicit field access operations.

This analysis is separate for each unit of work, and thus is applicableto incomplete programs. Indeed, a call graph can be built for each unitof work in isolation, the only requirement being a conservative model ofthe world beyond. The whole analysis is conservative given aconservative estimate of the types of the parameters for units of work.

The second step is to infer what code blocks in a unit of work needsynchronization, knowing what atomic sets it accesses. We associate asynthetic lock with each set, and synchronize on these. The objective isto find the smallest code block containing all the accesses for eachatomic set. In the case of a loop, which has accesses in its body, thismeans synchronizing the whole loop, since its body is potentiallyvisited more than once. The resulting code must be syntacticallycorrect. In particular, care must be taken so that local variabledeclarations are not separated from their uses. If a synchronized blockcontains a local variable declaration, then we expand it, so that alluses are in the same scope. Moreover, some code blocks accessingdifferent atomic sets may have statements in common. In this case, wejoin the code blocks into a single larger block that contains them all,and acquire all the necessary locks at the top. When multiple locks needto be acquired, we choose an arbitrary total order for them, and acquirethem in this order everywhere in the API, to avoid deadlock. It ispossible to further optimize these design choices. We could, forexample, refactor the code so that local variable declarations arepulled out of the synchronized blocks so that these do not need to beexpanded.

Most static and dynamic race detectors, as well as type systems andlanguages that guarantee race freedom are based on the common definitionof data races and therefore do not handle high-level races. Type systemsuse redundant annotations to verify that data races do not occur. Someknown approaches present a type system that allows the programmer toannotate each field with the lock that must protect it. In contrast, oursystem does not require the programmer to keep track of locks explicitlywhen adding synchronization to a class, and simplifies reasoning aboutconcurrency by inferring synchronized blocks.

An extension to ESC/Java detects a class of high-level data races,called “stale-value errors” and “Finding stale-value errors inconcurrent programs”. See SRC Technical Note 2002-004, May 2002. Thevalue of a local variable is stale if it is used beyond the criticalsection in which it was defined, and in particular if it is used inanother critical section. Scenario 1 of our definition of high-leveldata races addresses stale-value errors. However, it is presented in aframework that is independent of any implementation construct. The samescenario covers both low-level and high-level data races. Viewconsistency is a correctness criterion that ensures that multiple readsin a thread observe a consistent state. A view is defined to be the setof variables that a lock protects. Two threads are view consistent ifall the views in the execution of one, intersected with the maximal viewof the other, form a chain under set inclusion. View consistency can bechecked dynamically or statically. See C. Artho, K. Havelund, and A.Biere. “High-Level Data Races,” Workshop on Verification and Validationof Enterprise Information Systems (VVEIS), April 2003. Scenarios 8through 11 of our definition of high-level data races address the issueof multiple reads getting an inconsistent state. In our approach,however, the programmer indicates explicitly what sets of locations forman atomic set, so this information does not need to be extracted fromthe locking structure of the code. Our definition also covers a largerset of high level data races.

Atomicity is a non-interference property that has proven useful inreasoning about multi-threaded programs. See C. Flanagan and S. Qadeer.“A Type and Effect System for Atomicity”, Programming Language Designand Implementation (PLDI), 2003. An atomic code block can be assumed toexecute serially without interleaved steps from other threads. A numberof tools have been developed for checking atomicity violations,including static typing systems; dynamic analysis such as the Atomizerwhich combines Lipton's theory of reduction and ideas from dynamic racedetectors; and model checking techniques. Instead of marking code blocksas atomic, we mark sets of fields as forming atomic sets. Our definitionfor high level data races offers insight into why a code block needs tobe synchronized, and we infer synchronized blocks automatically withingiven units of work. From the programmer's perspective, a unit of workrestores atomic sets to consistent states when executed sequentially.When synchronization is added, units of work become atomic code blocksonly with respect to each atomic set. For each, there exists a serialorder for the units of work in an execution, but there may not be aglobal serial order across all of them. Reasoning about a multithreadedprogram is simplified by allowing the programmer to speak about theamount of concurrency in a declarative way. The programmer only needs tospecify which memory locations need to be manipulated atomically, andthe system takes care of adding synchronization anywhere it may beneeded in the API.

Our problematic interleaving scenarios are similar to those used by Wangand Stoller to provide run-time analyses for atomicity. See L. Wang andS. Stoller, “Run-Time Analysis for Atomicity”, In Proceedings of theThird Workshop on Runtime Verification (RV03), volume 89(2) ofElectronic Notes in Theoretical Computer Science. Elsevier, 2003. Ourscenarios are simpler, and more importantly they are complete, meaningthat an execution not displaying them is guaranteed to have a propertyrelated to serializability.

Software transactional memory is a method for alleviating the burden onthe programmer in determining which locks to hold, by allowing codeblocks to be marked as “atomic sections”. See T. Harris, S. Marlowe, S.Peyton-Jones, and M. Herlihy, “Composable Memory Transactions”,Proceedings of the ACM SIGPLAN Symposium on Principles and Practice ofParallel Programming (PPoPP), Chicago, Ill., June 2005. Animplementation of this method needs to guarantee that there exists aglobal serial order of execution for the atomic sections. This is ingeneral hard to implement in an imperative language, and requiresspecialized hardware. The requirement for our units of work is thatthere exists a serial order with respect to each atomic set, and theremay not be a global serial order. By weakening the guarantee, whilestill maintaining correctness, we have a method that is much easier toimplement.

Our definition of high-level data races is based on anomalies used tocharacterize levels of isolation in databases, and defined in the ANSISQL standard. See ANSI X3.135—1992, American National Standard forInformation Systems—Database Language—SQL, November 1992 and H.Berenson, P. Bernstein, J. Gray, J. Melton, E. O'Neil, P. O'Neil. “ACritique of ANSI SQL Isolation Levels”, In ACM SIGMOD InternationalConference on Management, May 1995. Commercial databases allowprogrammers to trade off consistency for performance by offeringdifferent levels of isolation. Each level is characterized by the set ofanomalies it does not allow. The highest level of isolation isserializability. Our definition is in terms of interleaving scenarios,similar to the schedules used to express the database anomalies. Some ofthese needed to be adapted because they explicitly talked about atransaction committing or aborting, and not all of them were directlyapplicable in the context of concurrent programming.

Atomic sets share characteristics with data groups. See K. R. M. Leino.“Data Groups: Specifying the Modification of Extended State”, In OOPSLA'98, 1998. Data groups solve a problem in the specification of methodswhose overrides may modify additional state introduced in subclasses.They represent a set of variables and can be listed in the ‘modifies’clause of a specification. A method that is allowed to modify a datagroup, is allowed to modify its downward closure, consisting of allmember variables added in subclasses. Atomic sets are similar in thatsubclasses may add locations to a set declared in a parent class. Theydiffer in that, unlike data groups, they are not hierarchical andnon-overlapping.

A publication by Deng et al. presents a method that allows the user tospecify synchronization patterns that are used to synthesizesynchronized code. See X. Deng, M. Dwyer, J. Hatcliff, and M. Mizuno.“Invariant-Based Specification, Synthesis, and Verification ofSynchronization in Concurrent Programs” in International Conference onSoftware Engineering (ICSE), May 2002. The generated code can then beverified using the Bandera toolset. The user must specify explicitly theregions of code that need synchronization, together with integerconstraints that represent patterns of synchronization between them.This is done by recording the number of threads that enter a region acode in a counter, as well as the number that exit. Synchronizationpatterns are then expressed using these counters. Our atomic setconstruct is simpler to express, and we do not require the user toindicate explicitly the regions that need synchronization. We also onlyfocus on one kind of synchronization pattern: exclusion between tworegions that access the same atomic set.

Referring to FIG. 8, there is shown a flow chart illustrating a highlevel information processing method 800 according to an embodiment ofthe invention. The method 800 comprises steps for ensuring consistencyof data and avoiding data races. In step 802 we examine a computerprogram written in an object-oriented language. In step 804 we receivesequences of accesses that form logical operations on a set of memorylocations. In step 806 we receive atomic sets of memory locations. Instep 808 we provide a message indicating where the synchronization isrequired.

Therefore, while there has been described what is presently consideredto be the preferred embodiment, it will be understood by those skilledin the art that other modifications can be made within the spirit of theinvention.

1. A method for ensuring consistency of data and preventing data races,the method comprising: receiving and examining a computer programwritten in an object-oriented language; receiving sequences of accessesthat form logical operations on a set of memory locations used by thecomputer program; receiving definitions of atomic sets of data from thememory locations, wherein said atomic sets are sets of data thatindicate an existence of a consistency property without requiring theconsistency property itself; inferring which code blocks of the computerprogram must be synchronized in order to prevent one or more data racesin the computer program, wherein synchronization is inferred bydetermining by analysis for each unit of work, what atomic sets are readand written by the unit of work, wherein said unit of work comprisesnon-overlapping threads; and providing a message indicating wheresynchronization is required.
 2. The method of claim 1, wherein receivingthe definitions of the atomic sets of data comprises receiving saiddefinitions from a programmer.
 3. The method of claim 1, wherein aprogrammer specifies the logical operations.
 4. The method of claim 1,wherein synchronization is inserted by using locks that block access todata that is being processed by the logical operation until that logicaloperation is concluded.
 5. The method of claim 1 further comprisingdetecting problematic interleaving scenarios for accesses to the atomicsets.
 6. The method of claim 5 wherein detecting the problematicinterleaving scenarios comprises detecting concurrent accesses that arelikely to produce the data race.
 7. The method of claim 1 furthercomprising a step of uncovering unnecessary synchronization.
 8. Themethod of claim 1 wherein inferring synchronization further comprises:assuming that each execution is such that every unit of work that writessome location in the atomic set, writes every location in that atomicset; and using the analysis to determine a smallest code blocksurrounding all reads and writes to the atomic set in the unit of work.9. The method of claim 8 further comprising adding synchronization tothe smallest code block.
 10. The method of claim 1 further comprising:ordering the units of work by occurrence of their first write events inan execution; and associating a unique timestamp with each unit of work.11. A method for detecting data races, the method comprising: receivingand examining a computer program written in an object-oriented language;receiving sequences of accesses that form logical operations on a set ofmemory locations used by the program; receiving definitions of atomicsets of the memory locations, wherein said atomic sets are sets of datathat indicate an existence of a consistency property without requiringthe consistency property itself; determining whether the logicaloperations operating on the sets of data are serializable per atomicset; and providing a message indicating any existing data races in thecode.
 12. The method of claim 11, wherein a programmer specifies theatomic sets.
 13. The method of claim 11, wherein a programmer specifiesthe logical operations.
 14. The method of claim 11 further comprisingdetecting problematic interleaving scenarios for accesses to the atomicsets.
 15. The method of claim 14 wherein detecting the problematicinterleaving scenarios comprises detecting concurrent accesses that arelikely to produce the data race.
 16. A computer-readable mediumcomprising instructions for: receiving and examining a computer programwritten in an object-oriented language; receiving sequences of accessesthat form logical operations on a set of memory locations used by theprogram; receiving definitions of atomic sets of data of the memorylocations, wherein said atomic sets of data are sets of data thatindicate an existence of a consistency property without requiring theconsistency property itself; inferring which code blocks of the computerprogram must be synchronized in order to prevent one or more data racesin the computer program, and providing a message indicating where thesynchronization is required.
 17. The medium of claim 16 whereininferring which code blocks must be synchronized comprises: determiningby analysis for each unit of work, what atomic sets are read and writtenby the unit of work, wherein the unit of work comprises non-overlappingthreads.
 18. The medium of claim 16 further comprising insertingsynchronization by using locks that block access to data that is beingprocessed by the logical operation until said logical operation isconcluded.
 19. The medium of claim 16 further comprising an instructionfor detecting problematic interleaving scenarios for accesses to atomicsets of data by detecting concurrent accesses that are likely to producethe data race.
 20. An information processing system comprising: acomputer program written in an object-oriented language; data comprisingatomic sets of memory locations used by the computer program, whereinsaid atomic sets are sets of data that indicate an existence of aconsistency property without requiring the consistency property itself;an inference engine for: analyzing accesses to the atomic sets;inferring which code blocks of the computer program must be synchronizedin order to prevent one or more data races in the computer program,wherein synchronization is inferred by determining by analysis for eachunit of work, what atomic sets are read and written by the unit of work,wherein said unit of work comprises non-overlapping threads; andproviding a message indicating where synchronization is required.